//
//  FKShoppingCartCell.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/18.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKShoppingCartCell.h"
#import <PPNumberButton.h>

@interface FKShoppingCartCell ()<PPNumberButtonDelegate>

@property (nonatomic, weak) UIButton *checkBtn;
/** 失效 */
@property (nonatomic, weak) UILabel *invalidLabel;
/** 商品图片*/
@property (nonatomic, weak) UIImageView *picView;
/** 商品类型 */
@property (nonatomic, weak) UILabel *typeLabel;
/** 商品名称*/
@property (nonatomic, weak) UILabel *nameLabel;
/** 商品规格*/
@property (nonatomic, weak) UILabel *specLabel;
/** 失效描述 */
@property (nonatomic, weak) UILabel *invalidDescLabel;
/** 商品价格*/
@property (nonatomic, weak) UILabel *priceLabel;
/** 数量操作*/
@property(nonatomic, weak) PPNumberButton *numBtn;

@end

@implementation FKShoppingCartCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIButton *checkBtn = [UIButton fk_btnWithTarget:self action:@selector(checkBtnClicked:) img:[UIImage imageNamed:@"bank_icon_select_n"]];
    [checkBtn setImage:[UIImage imageNamed:@"bank_icon_select_s"] forState:UIControlStateSelected];
    [self.contentView addSubview:checkBtn];
    self.checkBtn = checkBtn;
    
    UILabel *invalidLabel = [UILabel fk_labelWithFont:fk_adjustFont(11) textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter text:@"失效"];
    [self.contentView addSubview:invalidLabel];
    self.invalidLabel = invalidLabel;
    invalidLabel.backgroundColor = [UIColor fk_colorWithHex:0xB8B8B8];
    [invalidLabel fk_viewCornerRadius:fk_adjustW(9) borderWidth:0 borderColor:nil];
    invalidLabel.hidden = true;
    
    UIImageView *picView = [[UIImageView alloc] init];
    [self.contentView addSubview:picView];
    self.picView = picView;
    [picView fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    UILabel *typeLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter];
    [picView addSubview:typeLabel];
    self.typeLabel = typeLabel;
    typeLabel.backgroundColor = fkColorAlpha(0, 0, 0, 0.5);
    
    UILabel *nameLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    nameLabel.numberOfLines = 2;
    
    UILabel *specLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999];
    [self.contentView addSubview:specLabel];
    self.specLabel = specLabel;
    specLabel.numberOfLines = 2;
    
    UILabel *invalidDescLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999 text:@"商品已失效不能购买\n请联系商家"];
    [self.contentView addSubview:invalidDescLabel];
    self.invalidDescLabel = invalidDescLabel;
    invalidDescLabel.numberOfLines = 2;
    invalidDescLabel.hidden = true;
    
    UILabel *priceLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkPriceRed];
    [self.contentView addSubview:priceLabel];
    self.priceLabel = priceLabel;
    
    UIView *numContiner = [[UIView alloc] init];
    [self.contentView addSubview:numContiner];
    [numContiner fk_viewCornerRadius:0 borderWidth:0.8 borderColor:[UIColor fk_colorWithHex:0xD4D4D4]];
    
    PPNumberButton *numBtn = [PPNumberButton numberButtonWithFrame:CGRectZero];
    [numContiner addSubview:numBtn];
    self.numBtn = numBtn;
    numBtn.minValue = 1;
    numBtn.inputFieldFont = 15;
    numBtn.editing = NO;
    numBtn.increaseTitle = @"＋";
    numBtn.decreaseTitle = @"－";
    numBtn.delegate = self;
    numBtn.longPressSpaceTime = CGFLOAT_MAX;
    
    [picView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(fk_adjustW(105));
        make.left.offset(fk_adjustW(46));
        make.centerY.offset(0);
    }];
    [typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(15));
    }];
    [checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.offset(0);
        make.right.equalTo(picView.mas_left);
    }];
    [invalidLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(fk_adjustW(30));
        make.height.mas_equalTo(fk_adjustW(18));
        make.centerY.offset(0);
        make.right.equalTo(picView.mas_left).offset(fk_adjustW(5));
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(picView);
        make.left.equalTo(picView.mas_right).offset(8);
        make.right.offset(-12);
    }];
    [specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(picView);
        make.left.right.equalTo(nameLabel);
    }];
    [invalidDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(fk_adjustW(18));
    }];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(picView);
        make.left.equalTo(nameLabel);
    }];
    [numContiner mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(fk_adjustW(85));
        make.height.mas_equalTo(fk_adjustW(25));
        make.right.equalTo(nameLabel);
        make.bottom.equalTo(picView);
    }];
    [numBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
}

- (void)checkBtnClicked:(UIButton *)btn
{
    btn.selected = !btn.isSelected;
    
    NSMutableDictionary *dict = self.data;
    dict[@"selected"] = @(btn.isSelected);
    if ([self.delegate respondsToSelector:@selector(shoppingCartCell:didClickedCheckBtn:)]) {
        [self.delegate shoppingCartCell:self didClickedCheckBtn:btn];
    }
}

#pragma mark - PPNumberButtonDelegate
- (void)pp_numberButton:(PPNumberButton *)numberButton number:(NSInteger)number increaseStatus:(BOOL)increaseStatus
{
    if ([self.delegate respondsToSelector:@selector(shoppingCartCell:updateNum:type:)]) {
        [self.delegate shoppingCartCell:self updateNum:number type:increaseStatus ? @"1" : @"2"];
    }
}

- (void)setData:(id)data
{
    [super setData:data];
    
//    if ([data[@"status"] isEqualToString:@"1"]) {
//        self.invalidLabel.hidden = true;
//        self.invalidDescLabel.hidden = true;
//
//        self.checkBtn.hidden = false;
//        self.specLabel.hidden = false;
//        self.priceLabel.hidden = false;
//        self.numBtn.hidden = false;
//    } else {
//        self.invalidLabel.hidden = false;
//        self.invalidDescLabel.hidden = false;
//
//        self.checkBtn.hidden = true;
//        self.specLabel.hidden = true;
//        self.priceLabel.hidden = true;
//        self.numBtn.hidden = true;
//    }
    
    self.checkBtn.selected = [data[@"selected"] boolValue];
    [self.picView sd_setImageWithURL:[NSURL URLWithString:data[@"img"]] placeholderImage:kPlaceholder_1x1];
    self.typeLabel.text = [data[@"item_type"] isEqualToString:@"1"] ? @"核销商品" : @"物流商品";
    self.nameLabel.text = data[@"title"];
    self.specLabel.text = fkFORMAT(@"规格: %@", data[@"spec"]);
    self.priceLabel.text = fkFORMAT(@"￥%@", data[@"price"]);
    self.numBtn.currentNumber = [data[@"nums"] integerValue];
}

@end
